﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using System.Text;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class AccessoryInfoList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if (!base.CheckUserHasFuction(1, "Func3051"))
                {
                    spanAddDuty.Visible = false;
                }

                ddlVendor.BindDDDW(new CommonBll().dddw("vendor"), "vendor", "vendor");
                ddlVendor.Items.Add(new ListItem("--不限--", "0"));
                ddlVendor.SelectedValue = "0";

                BindDuty(AspNetPager1.CurrentPageIndex);

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDuty(AspNetPager1.CurrentPageIndex);
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                
                SystemBll bll = new SystemBll();
                bll.Delete<WsmAccessoryInfoEntity>(" and AccessoryID='" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["AccessoryID"].ToString() + "'");
            }
            BindDuty(AspNetPager1.CurrentPageIndex);
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label AccessoryID = e.Item.FindControl("AccessoryID") as Label;
                Label AccessoryType = e.Item.FindControl("AccessoryType") as Label;
                Label AccessoryTypeName = e.Item.FindControl("AccessoryTypeName") as Label;
                LinkButton lnkbtnDelete = e.Item.FindControl("lnkbtnDelete") as LinkButton;

                Label relateInstrument = e.Item.FindControl("relateInstrument") as Label;
                List<WsmInstrumentInfoEntity> list = new SystemBll().GetAll<WsmInstrumentInfoEntity>(" and InstrumentID in ( select InstrumentID from Cms_Wsm_AccessoryInInstrument where AccessoryID='" + AccessoryID.Text + "')");
                String instruments = "";
                foreach(WsmInstrumentInfoEntity entity in list){
                    if (instruments != "") {
                        instruments += "/";
                    }
                    instruments += entity.instrumentName;
                }
                relateInstrument.Text = instruments;
                AccessoryTypeName.Text = AccessoryType.Text == "1" ? "配件" : "易耗件";

                if (!base.CheckUserHasFuction(1, "Func3051") && lnkbtnDelete!=null)
                {
                    lnkbtnDelete.Visible = false;
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindDuty(AspNetPager1.CurrentPageIndex);
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindDuty(AspNetPager1.CurrentPageIndex);

        }
        private void BindDuty(int pageindex)
        {
            string Whereclause = "";

            if (txtCompanyInstrumentCode.Text.Trim() != "")
            {
                Whereclause += " and CompanyInstrumentCode like '" + txtCompanyInstrumentCode.Text.Trim() + "%'";
            }

            if (ddlVendor.SelectedValue.ToString() !="0")
            {
                Whereclause += " and vendor = '" + ddlVendor.SelectedValue.ToString() + "'";
            }

            if (ddlAccessoryType.SelectedValue.ToString() != "0")
            {
                Whereclause += " and AccessoryType = " + ddlAccessoryType.SelectedValue.ToString() + "";

            }
            int RecordCount = 0;

            List<WsmAccessoryInfoEntity> list = new SysUserBll().GetAll<WsmAccessoryInfoEntity>(Whereclause, "Vendor asc", AspNetPager1.PageSize, pageindex, out RecordCount);

            AspNetPager1.RecordCount = RecordCount;
            gridAccessoryList.DataSource = list;
            gridAccessoryList.DataBind();
        }
    }
}
